<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * This file contains the Domain_Store class.
 *
 * PHP version 5
 *
 * This file is part of Majic - A rapid-development web application framework
 * for PHP 5.
 *
 * Majic is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * Majic is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Majic. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     Domain
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 * @copyright   2007-2008 Michael J. I. Jackson
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL 3.0
 * @version     SVN: $Id: Store.php 34 2008-07-19 04:50:53Z mjijackson $
 */

/**
 * The base class for all domain stores. A domain store serves as an identity
 * map for domain objects.
 *
 * @package     Domain
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 */
abstract class Domain_Store
{

    /**
     * Determines whether or not all domain objects in the domain map will be
     * automatically saved when this object destructs (at the end of the
     * request).
     *
     * @var     bool
     * @access  protected
     */
    protected $auto_save = false;

    /**
     * Constructor.
     *
     * @access  public
     */
    public function __construct() {}

    /**
     * Adds the given object to the domain map if it doesn't already exist.
     *
     * @param   string          $key        The unique key to use for the object
     * @param   Domain_Object   $obj        The domain object
     * @return  void
     * @access  public
     * @abstract
     */
    abstract public function add($key, Domain_Object $obj);

    /**
     * Unconditionally adds the given object to the domain map.
     *
     * @param   string          $key        The unique key to use for the object
     * @param   Domain_Object   $obj        The domain object
     * @return  void
     * @access  public
     * @abstract
     */
    abstract public function set($key, Domain_Object $obj);

    /**
     * Finds an object with the given class and id in the domain map. If the
     * object cannot be found, a null value will be returned.
     *
     * @param   string          $key        The unique key for the object
     * @return  Domain_Object               The domain object with the given
     *                                      class and id
     * @access  public
     * @abstract
     */
    abstract public function find($key);

    /**
     * Saves each object in this store.
     *
     * @return  void
     * @access  public
     */
    abstract public function save();

    /**
     * Enables automatic saving of all domain objects.
     *
     * @return  void
     * @access  public
     */
    public function enableAutosave()
    {
        $this->auto_save = true;
    }

    /**
     * If the auto-save setting is enabled, this function will automatically
     * call Domain::saveAll() at the end of the request.
     *
     * @return  void
     * @access  public
     */
    public function __destruct()
    {
        if ($this->auto_save) {
            $this->save();
        }
    }

}

?>
